<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="950" destroy-on-close @closed="$emit('closed')">
		<el-main>
			<el-row>
				<el-table :data="[info]">
					<el-table-column label="序号" type="index" width="100"></el-table-column>
					<el-table-column label="订单编号" prop="company_order_sn" width="100"></el-table-column>
					<el-table-column label="企业客户名称" prop="company_name" width="110"></el-table-column>
					<el-table-column label="采购模式" prop="" width="100">集采</el-table-column>
					<el-table-column label="服务卡名称" prop="card_name" width="150"></el-table-column>
					<el-table-column label="商品名称" prop="card_product_name" width="150"></el-table-column>
<!--					<el-table-column label="商品类型" prop="" width="100">视频问诊</el-table-column>-->
					<el-table-column label="采购数量" prop="purchase_qty" width="100"></el-table-column>
					<el-table-column label="有效期" prop="valid_time" width="140"></el-table-column>
					<el-table-column label="下单时间" prop="created_at" width="150"></el-table-column>
				</el-table>
			</el-row>
		</el-main>
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm"
				 label-width="110px"
				 label-position="left">
			<el-form-item label="付款方式" prop="pay_type">
				<el-select v-model="form.pay_type" filterable style="width: 100%" clearable>
					<el-option v-for="item in payTypeList" :key="item.pay_type" :label="item.dict_sub_val" :value="item.dict_sub_val"/>
				</el-select>
			</el-form-item>
			<el-form-item label="付款金额" prop="pay_amount">
				<el-input v-model="form.pay_amount" placeholder="请输入实际付款金额，金额必须大于0" clearable></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">确 认</el-button>
		</template>
	</el-dialog>
</template>

<script>

export default {
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "pay",
			titleMap: {
				pay: '付款',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
				company_order_sn: "",
				pay_type: "",
				pay_amount: "",
			},
			info: [],
			//验证规则
			rules: {
				pay_type: [
					{required: true, message: '请选择付款方式', trigger: 'change'}
				],
				pay_amount: [
					{required: true, message: '请输入实际付款金额，数量必须是大于0'},
				],
			},
			payTypeList: [],
		}
	},
	mounted() {
		this.getPayTypeList()
	},
	methods: {
		getPayTypeList() {
			this.$API['company.company_order'].getPayTypeList.get().then((res) => {
				if (res.code == 0) {
					this.payTypeList = res.data;
				}
			}).catch((msg) => {
				this.$alert(msg, "提示", {type: 'error'})
			})
		},
		//显示
		open(mode = 'pay') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					this.$API['company.company_order'].payOrder.post(this.form)
						.then((res) => {
							if (res.code == 0) {
								this.$emit('success', this.form, this.mode)
								this.$message.success("操作成功")
							} else {
								this.$alert(res.message, "提示", {type: 'error'})
							}
						})
						.catch((msg) => {
							this.$alert(msg, "提示", {type: 'error'})
						})
						.finally(() => {
							this.isSaveing = false;
						});
				} else {
					return false;
				}
			})
		},
		//表单注入数据
		setData(data = []) {
			this.info = data;
			this.form.company_order_sn = data.company_order_sn
			//可以和上面一样单个注入，也可以像下面一样直接合并进去
			//Object.assign(this.form, data)
		}
	}
}
</script>

<style>
</style>
